package com.cy.pj.sys.service.impl;

import com.cy.pj.common.exception.ServiceException;
import com.cy.pj.sys.Dao.CGBInformationDao;
import com.cy.pj.sys.pojo.CGBInformation;
import com.cy.pj.sys.service.CGBInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class CGBInformationServiceImpl implements CGBInformationService {

    @Autowired
    private CGBInformationDao cgbInformationDao;

    @Override
    public List<CGBInformation> selectUsers(CGBInformation cgbInformation) {
        return cgbInformationDao.selectUsers(cgbInformation);
    }

//    @Override
//    public int validById(Integer id, Integer valid) {
//        CGBInformation information=(CGBInformation) SecurityUtils.getSubject().getPrincipal();//后续登录用户
//        String modifiedUser=information.getUsername();
//        int rows=cgbInformationDao.validById(id, valid,modifiedUser);
//        if(rows==0)
//            throw new ServiceException("记录不存在");
//        return rows;
//    }

    @Override
    public int insertUser(CGBInformation cgbInformation) {
//        String salt= UUID.randomUUID().toString();
//        SimpleHash sh=new SimpleHash("MD5",cgbInformation.getPassword(),salt,1);
//        String hashedPassword=sh.toHex();
//        cgbInformation.setSalt(salt);
//        cgbInformation.setPassword(hashedPassword);
        int rows=cgbInformationDao.insertUser(cgbInformation);

        return rows;
    }

    @Override
    public CGBInformation selectById(Integer id) {
        CGBInformation information=cgbInformationDao.selectById(id);
        if(information==null)
            throw new ServiceException("用户不存在");
        return information;
    }

    @Override
    public int updateUser(CGBInformation cgbInformation) {
        int rows=cgbInformationDao.updateUser(cgbInformation);
        if(rows==0){
            throw new ServiceException("记录不存在");
        }
        return rows;
    }

}
